package com.zm.demo.doc;

import org.reflections.Reflections;
import org.reflections.scanners.Scanners;

import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author z01140
 * @Package: com.smy.nct.service
 * @Description:
 * @CreateDate 2022/10/17 16:46
 * @UpdateDate 2022/10/17 16:46
 */
public class ApiDocService {

    /**
     * 接口包名
     */
    private String packageName;


//    public static void main(String[] args) throws ClassNotFoundException, InterruptedException {
//        Reflections reflections = new Reflections("com.zm.demo.doc.service", Scanners.values());
//
//        Map<String, Set<String>> map =  reflections.getStore().get("SubTypes");
//        Set<Class<?>> classSet =  reflections.getTypesAnnotatedWith(ApiClassDesc.class);
//
//        Iterator<Class<?>> it = classSet.iterator();
//        while (it.hasNext()){
//            Class clazz = it.next();
//            System.err.println();
//            System.err.println();
//            System.err.println("类名：" + clazz.getName());
//            int methodNo = 0;
//            for (Method declaredMethod : clazz.getDeclaredMethods()) {
//                String methodName = declaredMethod.getName();
//                System.out.println(String.format("方法%d,名称：%s:" , methodNo++, methodName));
//                Class returnClazz = declaredMethod.getReturnType();
//                String returnName = returnClazz.getName();
//                System.out.println("返回类型:" + returnName);
//                Field[] returnFileds = returnClazz.getDeclaredFields();
//                Parameter[] parameters = declaredMethod.getParameters();
//                Class[] parameters1 = declaredMethod.getParameterTypes();
//                TypeVariable<Method>[] typeVariables = declaredMethod.getTypeParameters();
//                for (TypeVariable<Method> typeVariable : typeVariables) {
//                    Type[] types = typeVariable.getBounds();
//                }
//                doParameter(parameters);
//            }
//            Thread.sleep(20L);
//        }
//
//
//    }

    /**
     * 获取注解信息
     * @param parameters 参数数组
     * @date 202210211842
     */
    public static void doParameter(Parameter[] parameters){
        if (parameters != null){
            int i = 0;
            for (Parameter parameter : parameters) {
                Class clazz = parameter.getType();
                ApiModelDesc annotation = (ApiModelDesc)clazz.getAnnotation(ApiModelDesc.class);
                System.out.println(String.format("参数%s类型：%s", parameter.getName(), clazz.getName()));
                System.out.println(String.format("参数%s描述：%s", parameter.getName(), annotation.value()));
                if (annotation != null){
                    Field[] fields = clazz.getDeclaredFields();
                    if (fields != null){
                        int fieldSeq = 0;
                        for (Field field : fields) {
                            fieldSeq++;
                            ApiModelDesc fieldAnnotation = (ApiModelDesc)field.getAnnotation(ApiModelDesc.class);
                            System.out.println(String.format("参数%s类型：%s", field.getName(), field.getClass().getName()));
                            System.out.println(String.format("参数%s描述：%s", field.getName(), fieldAnnotation.value()));
                        }
                    }
                }
            }
        }
    }


//    /**
//     * 测试111
//     * @throws Exception
//     */
//    public void t() throws Exception{
//        Reflections reflections = new Reflections("com.zm.demo.doc.service", Scanners.values());
//
//        Map<String, Set<String>> map =  reflections.getStore().get("SubTypes");
//        Set<Class<?>> classSet =  reflections.getTypesAnnotatedWith(ApiClassDesc.class);
//        Iterator<Map.Entry<String, Set<String>>> iterator = map.entrySet().iterator();
//        while (iterator.hasNext()){
//            Map.Entry<String, Set<String>> entry = iterator.next();
//            if (!entry.getKey().contains("com.smy.nct.service")){
//                iterator.remove();
//            }
//        }
//
//        Iterator<Class<?>> it = classSet.iterator();
//        Iterator<Map.Entry<String, Set<String>>> iterator1 = map.entrySet().iterator();
//        while (iterator1.hasNext()){
//            Map.Entry<String, Set<String>> entry = iterator1.next();
//            Class clazz = ClassLoader.getSystemClassLoader().loadClass(entry.getKey());
//            System.err.println("类名：" + clazz.getName());
//            int methodNo = 0;
//            for (Method declaredMethod : clazz.getDeclaredMethods()) {
//                System.out.println();
//                String methodName = declaredMethod.getName();
//                System.out.println(String.format("方法%d,名称：%s:" , methodNo++, methodName));
//                Class returnClazz = declaredMethod.getReturnType();
//                String returnName = returnClazz.getName();
//                System.out.println("返回类型:" + returnName);
//                Field[] returnFileds = returnClazz.getDeclaredFields();
//                Parameter[] parameters = declaredMethod.getParameters();
//                Class[] parameters1 = declaredMethod.getParameterTypes();
//                TypeVariable<Method>[] typeVariables = declaredMethod.getTypeParameters();
//                for (TypeVariable<Method> typeVariable : typeVariables) {
//                    Type[] types = typeVariable.getBounds();
//                }
//                doParameter(parameters);
//            }
//            Thread.sleep(20L);
//        }
//
//
//    }
}
